package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 *
 * @author ttao
 * @since 2023/4/24 11:13
 */

@Api(tags = "菜品管理接口")
@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 菜品管理分页条件查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品管理分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> queryPage(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品查询：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.queryPage(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 添加菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result<String> save(@RequestBody DishDTO dishDTO) {
        return dishService.saveWithFlavor(dishDTO) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 菜品批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation("菜品批量删除")
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam("ids") List<Long> ids) {

        return dishService.deleteByIds(ids) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 根据id查询菜品和对应的口味数据
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品")
    @GetMapping("{id}")
    public Result<DishVO> selectById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.selectByIdWithFlavor(id);
        return dishVO != null ? Result.success(dishVO) : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> updateById(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        return dishService.updateWithFlavor(dishDTO) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    @ApiOperation("启用、禁用菜品")
    @PostMapping("/status/{status}")
    public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        log.info("更新菜品启用/禁用状态:{}-{}", status, id);
        return dishService.startOrStop(status, id) > 0 ? Result.success() : Result.error(MessageConstant.UNKNOWN_ERROR);
    }

    /**
     * 根据分类id查询对应菜品
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<DishVO>> list(Long categoryId) {
        List<DishVO> dishList = dishService.selectByCategoryId(categoryId);
        return Result.success(dishList);
    }
}
